﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace Plugin
{
    public class Config
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        private string _ServiceName = "14所物资代码后台监视";

        public string ServiceName
        {
            get
            {
                return _ServiceName;
            }
        }

        /// <summary>
        /// 插件版本
        /// </summary>
        private string _DLLVersion = "1.0";

        public string DLLVersion
        {
            get
            {
                return _DLLVersion;
            }
        }

        /// <summary>
        /// 运行间隔，默认60秒执行一次
        /// </summary>
        private int _RunInterval = 60;

        public int RunInterval
        {
            get
            {
                return _RunInterval;
            }
        }

        /// <summary>
        /// 显示设置窗体
        /// </summary>
        public void ShowConfig()
        {
            frmConfig newFrmConfig = new frmConfig();
            newFrmConfig.ShowDialog();
        }
    }

    public class Service
    {
        private SqlTools sqt;

        public Service()
        {
            //初始化SQL查询对象
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
            //检查配置文件
            CheckIniFile();
        }

        public void Start()
        {
            string strStartDate = string.Empty;
            string strEndDate = string.Empty;
            StringBuilder tmpsb = new StringBuilder();
            GeneralTools.GetPrivateProfileString("Config", "StartDate", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Monitor_Wz_14_Config.ini");
            strStartDate = tmpsb.ToString();
            GeneralTools.GetPrivateProfileString("Config", "EndDate", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Monitor_Wz_14_Config.ini");
            strEndDate = tmpsb.ToString();
            //检查当前是否有14所订货合同，但物资代码清单中未包含该项信息的
            string strTmpSelectString = "select distinct htb003 公司型号规格 from hta_table ,htb_table where hta001 = htb001 and hta014 = 'Y' and hta026 = '603车间' and hta003 like '08-14%' and hta002 between '@起始日期' and '@截止日期' and not exists(select * from DefualtDatabase.dbo.CustomerMaterialNoMapping where CustomerID = '08-14' and MyModel collate Chinese_PRC_BIN = htb003)";
            //if (sqt.RecordExist(strTmpSelectString .Replace("@起始日期",strStartDate )
            //                                                            .Replace ("@截止日期",strEndDate )))
            if (sqt.RecordExist(strTmpSelectString.Replace("@起始日期", "20170101")
                                                                        .Replace("@截止日期", "20171231")))
            {
                frmConfig newFrmConfig = new frmConfig();
                newFrmConfig.ShowDialog();                
            }
        }

        /// <summary>
        /// 检查配置文件是否存在
        /// </summary>
        private void CheckIniFile()
        {
            FileInfo fi = new FileInfo(System.Windows.Forms.Application.StartupPath + "\\Monitor_Wz_14_Config.ini");
            if (!fi.Exists)
            {
                //获取服务器当前日期
                string strDate = GeneralTools.GetString(sqt.DoCmdScalarData("select convert(varchar(10),GetDate(),112)"),string.Empty );
                //创建配置文件
                fi.Create();
                //写入配置
                GeneralTools.WritePrivateProfileString("Config", "StartDate", strDate, fi.FullName);
                GeneralTools.WritePrivateProfileString("Config", "EndDate", strDate, fi.FullName);
            }
        }
    }
}
